package com.wyp.didishige.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wyp.didishige.utils.FaceTextUtils;
import com.wyp.didishige.R;
import com.wyp.didishige.utils.ImageLoadOptions;
import com.wyp.didishige.utils.TimeUtil;

import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMMessageType;

/** 会话适配器
  * @ClassName: MessageRecentAdapter
  * @Description: TODO
  * @author smile
  * @date 2014-6-7 下午2:34:10
  */
public class MessageRecentAdapter extends ArrayAdapter<BmobIMConversation> implements Filterable{
	
	private LayoutInflater inflater;
	private List<BmobIMConversation> mData;
	private Context mContext;
	
	public MessageRecentAdapter(Context context, int textViewResourceId, List<BmobIMConversation> objects) {
		super(context, textViewResourceId, objects);
		inflater = LayoutInflater.from(context);
		this.mContext = context;
		mData = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final BmobIMConversation item = mData.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_conversation, parent, false);
		}
		ImageView iv_recent_avatar = ViewHolder.get(convertView, R.id.iv_recent_avatar);
		TextView tv_recent_name = ViewHolder.get(convertView, R.id.tv_recent_name);
		TextView tv_recent_msg = ViewHolder.get(convertView, R.id.tv_recent_msg);
		TextView tv_recent_time = ViewHolder.get(convertView, R.id.tv_recent_time);
		TextView tv_recent_unread = ViewHolder.get(convertView, R.id.tv_recent_unread);
		
		//填充数据
		String avatar = item.getConversationIcon();
		if(avatar!=null&& !avatar.equals("")){
			ImageLoader.getInstance().displayImage(avatar, iv_recent_avatar, ImageLoadOptions.avatarOptions);
		}else{
			ImageLoader.getInstance().displayImage("drawable://"+R.drawable.ic_launcher, iv_recent_avatar, ImageLoadOptions.avatarOptions);
		}
		
		tv_recent_name.setText(item.getConversationTitle());

		List<BmobIMMessage> msgs =item.getMessages();

		if(msgs!=null && msgs.size()>0){
			BmobIMMessage lastMsg =msgs.get(0);
			String content =lastMsg.getContent();
			if(lastMsg.getMsgType().equals(BmobIMMessageType.TEXT.getType()) || lastMsg.getMsgType().equals("agree")){
				SpannableString spannableString = FaceTextUtils
						.toSpannableString(mContext, content);
				tv_recent_msg.setText(spannableString);
			}else if(lastMsg.getMsgType().equals(BmobIMMessageType.IMAGE.getType())){
				tv_recent_msg.setText("[图片]");
			}else if(lastMsg.getMsgType().equals(BmobIMMessageType.VOICE.getType())){
				tv_recent_msg.setText("[语音]");
			}else if(lastMsg.getMsgType().equals(BmobIMMessageType.LOCATION.getType())){
				tv_recent_msg.setText("[位置]"+content);
			}else if(lastMsg.getMsgType().equals(BmobIMMessageType.VIDEO.getType())){
				tv_recent_msg.setText("[视频]");
			}else{//开发者自定义的消息类型，需要自行处理
				tv_recent_msg.setText("[未知]");
			}
			tv_recent_time.setText(TimeUtil.getChatTime(lastMsg.getCreateTime()));
		}else{//防止消息错乱
			tv_recent_msg.setText("");
			tv_recent_time.setText("");
		}

		long num = BmobIM.getInstance().getUnReadCount(item.getConversationId());
		if (num > 0) {
			tv_recent_unread.setVisibility(View.VISIBLE);
			tv_recent_unread.setText(num + "");
		} else {
			tv_recent_unread.setVisibility(View.GONE);
		}
		return convertView;
	}

}
